package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.dao.*;
import com.bootdo.wx.domain.*;
import com.bootdo.wx.service.MemberUpgradeService;
import com.bootdo.wx.service.MemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.wx.service.MemberPriceService;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Service
public class MemberPriceServiceImpl implements MemberPriceService {
	@Autowired
	private MemberPriceDao memberPriceDao;
	@Autowired
	private CmsContentDao cmsContentDao;
	@Autowired
	private MemberScoreDao memberScoreDao;
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private OrderDetailDao orderDetailDao;
	@Autowired
	private MemberUpgradeService memberUpgradeService;
	@Autowired
	private MemberLevelChangeDao memberLevelChangeDao;
	
	@Override
	public MemberPriceDO get(String id){
		return memberPriceDao.get(id);
	}
	
	@Override
	public List<MemberPriceDO> list(Map<String, Object> map){
		return memberPriceDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberPriceDao.count(map);
	}
	
	@Override
	public int save(MemberPriceDO memberPrice){
		return memberPriceDao.save(memberPrice);
	}
	
	@Override
	public int update(MemberPriceDO memberPrice){
		return memberPriceDao.update(memberPrice);
	}
	
	@Override
	public int remove(String id){
		return memberPriceDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberPriceDao.batchRemove(ids);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public int czSuccse(String priceId) {
		MemberPriceDO priceDO = memberPriceDao.get(priceId);
		String userId = priceDO.getUserId();
		MemberUserDO userDO = memberUserDao.get(userId);
		BigDecimal payment = priceDO.getPrice();   //单笔消费金额
		Date date = new Date();
		if (priceDO.getDelFlag() == 1) {
			priceDO.setDelFlag(0);
			memberPriceDao.update(priceDO);
			//判断升级
			int level = userDO.getLevel();
			int newLevel = 0;
			if (level > 0) {
				newLevel = 1;
			}
			//查询升级数据
			Map<String,Object> map = new HashMap<String,Object>();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			map.put("userId", userId);
			Date Eight = DateUtils.subDayOfDate(DateUtils.getDayBegin(), 180);  //180天前
			Date nine = DateUtils.subDayOfDate(DateUtils.getDayBegin(), 90);  //90天前
			map.put("sTime", format.format(Eight));
			BigDecimal eightNum = memberUserDao.getShoppingNum(map);  //180天的消费值
			map.put("sTime", format.format(nine));
			BigDecimal nineNum = memberUserDao.getShoppingNum(map);  //90天的消费值
			map.clear();
			map.put("userId", userId);
			map.put("level", new int[]{1, 2, 3});
			int hyNum = memberUserDao.getZtChildrenNUm(map);   //直推会员人数
			map.put("level",new int[]{4});
			int cfNum = memberUserDao.getZtChildrenNUm(map);   //直推初级分销人数
			//判断是否满足初级会员
			MemberUpgradeDO upgradeDO1 = memberUpgradeService.getByLevel("1");
			if (eightNum.compareTo(upgradeDO1.getAllPrice()) != -1) { //180天金额满足
				newLevel = 1;
			}
			if (payment.compareTo(upgradeDO1.getOnePrice()) != -1) {  //单笔金额满足
				newLevel = 1;
			}
			//判断是否满足中级会员
			MemberUpgradeDO upgradeDO2 = memberUpgradeService.getByLevel("2");
			if (eightNum.compareTo(upgradeDO2.getAllPrice()) != -1) { //180天金额满足
				newLevel = 2;
			}
			if (payment.compareTo(upgradeDO2.getOnePrice()) != -1) {  //单笔金额满足
				newLevel = 2;
			}
			//判断是否满足高级会员
			MemberUpgradeDO upgradeDO3 = memberUpgradeService.getByLevel("3");
			if (eightNum.compareTo(upgradeDO3.getAllPrice()) != -1) { //180天金额满足
				newLevel = 3;
			}
			if (payment.compareTo(upgradeDO3.getOnePrice()) != -1) {  //单笔金额满足
				newLevel = 3;
			}
			//判断是否满足初级分销
			MemberUpgradeDO upgradeDO4 = memberUpgradeService.getByLevel("4");
			if (nineNum.compareTo(upgradeDO4.getAllPrice()) != -1) { //金额满足
				if (hyNum >= 10) {  //满足推广要求
					newLevel = 4;
				}
			}
			if (hyNum >= 20) {  //满足单独推广要求
				newLevel = 4;
			}
			//判断是否满足高级分销
			MemberUpgradeDO upgradeDO5 = memberUpgradeService.getByLevel("5");
			if (nineNum.compareTo(upgradeDO5.getAllPrice()) != -1) { //金额满足
				if (cfNum >= 1 && hyNum >= 10) {  //满足推广要求
					newLevel = 5;
				}
			}
			if (cfNum >= 3 && hyNum >= 20) {  //满足单独推广要求
				newLevel = 5;
			}
			//等级变化
			if (level > 3 && newLevel< level) {  //分销不降级
				newLevel = level;
			}
			if (newLevel != level) {
				MemberUserDO memberUserDO = new MemberUserDO();
				memberUserDO.setId(userId);
				memberUserDO.setLevel(newLevel);
				memberUserDao.update(memberUserDO);
				//添加等级变动记录
				MemberLevelChangeDO changeDO = new MemberLevelChangeDO();
				changeDO.setId(UuidUtil.get32UUID());
				changeDO.setOldLevel(level);
				changeDO.setNewLevel(newLevel);
				changeDO.setCreateAt(date);
				changeDO.setUpdateAt(date);
				changeDO.setDelFlag(0);
				changeDO.setUserId(userId);
				changeDO.setComent("180天的消费值："+eightNum+"，90天的消费值："+nineNum+"，单笔金额："+payment+"，直推会员人数："+hyNum+"，直推初级分销人数："+cfNum);
				memberLevelChangeDao.save(changeDO);
			}


			//添加积分
			/*String value = cmsContentDao.getByType("score-recharge").get(0).getValue();
			BigDecimal scoreNumber = priceDO.getPrice().multiply(new BigDecimal(value)).divide(new BigDecimal(100));
			MemberScoreDO scoreDO = new MemberScoreDO();
			scoreDO.setId(UuidUtil.get32UUID());
			scoreDO.setGId(priceId);
			scoreDO.setUserId(priceDO.getUserId());
			scoreDO.setType(2);
			scoreDO.setMoney(scoreNumber);
			scoreDO.setCreateAt(date);
			scoreDO.setUpdateAt(date);
			scoreDO.setDelFlag(0);
			memberScoreDao.save(scoreDO);*/
		}
		return 0;
	}
	
}
